var express = require('express');
var router = express.Router();

var md5 = require("md5");
var fs=require("fs");
var conn = require('../conn');
var user_tab = require('../user_tab');

/* GET home page. */
router.get('/', function(req, res, next) {
 var ut= user_tab(req);
 var sql = "select * from product where cata_id=1 limit 8";
  conn.query(sql,function(err,rows,fields){
    var flower=rows;
    conn.query("select * from product where cata_id=3 limit 8",function(err,rows,fields){
      var zhang=rows;
      conn.query("select * from product where type_id=2 limit 8",function(err,rows,fields){
       var yong=rows;
        conn.query("select * from product where type_id=3 limit 8",function(err,rows,fields){
          var cake=rows;
          conn.query("select * from product where type_id=4 limit 8",function(err,rows,fields){
            var gift=rows;
            res.render('index', {flower: flower,zhang:zhang,yong:yong, cake:cake,gift:gift,usertab:ut.usertab,in_out:ut.in_out,link1:ut.link1,link2:ut.link2,order:ut.order});
          })
        })
      })
    });
  });
});
/*鲜花模块*/
router.get('/flower', function(req, res, next) {
  conn.query("select count(*) as c from product where type_id=1",function(err,rows,fields){
    req.flower_num = rows[0].c;
    next();
  });
},function(req, res, next){
  var ut= user_tab(req);
  if (req.query.page == undefined) {
    var page=1
  }else{
    page = req.query.page;
  }
  var pageSize = 8;
  var pageLength = Math.ceil(req.flower_num/pageSize);
  var sql = "select * from product where type_id=1 limit "+(page-1)*pageSize+","+pageSize;
  conn.query("select * from producttype",function(err,rows,fields){
    var type = rows;
    conn.query(sql,function(err,rows,fields){
      var urlList = req.url.substr(0,5);
      res.render('flower', { flower: rows,type:type,url:urlList,page:pageLength,page_id:page-1 ,flower_num:req.flower_num,usertab:ut.usertab,in_out:ut.in_out,link1:ut.link1,link2:ut.link2,order:ut.order});
    })
  })
});
router.get('/flowerList', function(req, res, next) {
  conn.query("select count(*) as c from product where cata_id="+req.query.cata_id,function(err,rows,fields){
    req.flower_num = rows[0].c;
    next();
  });
},function(req, res, next){
  var ut= user_tab(req);
  if (req.query.page == undefined) {
    var page=1
  }else{
    page = req.query.page;
  }
  var pageSize = 8;
  var pageLength = Math.ceil(req.flower_num/pageSize);
  var sql = "select * from product where cata_id="+req.query.cata_id+" limit "+(page-1)*pageSize+","+pageSize;
  conn.query("select * from producttype",function(err,rows,fields){
    var type = rows;
    conn.query(sql,function(err,rows,fields){
      var urlList = req.url.substr(0,5);
      res.render('flowerList', { flower: rows ,type:type,url:urlList,page:pageLength,page_id:page-1,flower_num:req.flower_num,cata_id:req.query.cata_id,usertab:ut.usertab,in_out:ut.in_out,link1:ut.link1,link2:ut.link2,order:ut.order});
    })
  })
});

/*永生花模块*/
router.get('/yongshenghua', function(req, res, next) {
  var ut= user_tab(req);
  var sql = "select * from product where cata_id=6 limit 3";
  conn.query(sql,function(err,rows,fields){
    var yong_box=rows;
    conn.query("select * from product where cata_id=7 limit 3",function(err,rows,fields){
      var yong_large=rows;
      conn.query("select * from product where cata_id=8 limit 3",function(err,rows,fields){
       var yong_xun=rows;
        conn.query("select * from product where cata_id=9 limit 3",function(err,rows,fields){
          var yong_vase=rows;
          conn.query("select * from product where cata_id=10 limit 3",function(err,rows,fields){
            var yong_te=rows;
            conn.query("select * from producttype",function(err,rows,fields){
              var type = rows;
              var urlList = req.url.substr(0,5);
              res.render('yongshenghua', { type:type,url:urlList,yong_box: yong_box,yong_large:yong_large,yong_xun:yong_xun, yong_vase:yong_vase,yong_te:yong_te,usertab:ut.usertab,in_out:ut.in_out,link1:ut.link1,link2:ut.link2,order:ut.order});
            })
          })
        })
      })
    });
  });
});
router.get('/yongshenghuaList', function(req, res, next) {
  var ut= user_tab(req);
  conn.query("select * from catalog where ptype_id=2",function(err,rows,fields){
    var cata = rows;
    var sql = "select * from product where cata_id="+req.query.cata_id;
    conn.query("select * from producttype",function(err,rows,fields){
      var type = rows;
      conn.query(sql,function(err,rows,fields){
        var urlList = req.url.substr(0,5);
        res.render('yongshenghuaList', { item: rows ,type:type,url:urlList,cata:cata,cata_id:req.query.cata_id,usertab:ut.usertab,in_out:ut.in_out,link1:ut.link1,link2:ut.link2,order:ut.order});
      })
    })
  })
});
/*蛋糕模块*/
router.get('/cake', function(req, res, next) {
  var ut= user_tab(req);
  var sql = "select * from product where cata_id=20 limit 4";
  conn.query(sql,function(err,rows,fields){
    var yuanzu=rows;
    conn.query("select * from product where cata_id=21 limit 4",function(err,rows,fields){
      var haoli=rows;
      conn.query("select * from product where cata_id=22 limit 4",function(err,rows,fields){
        var xingfu=rows;
        conn.query("select * from producttype",function(err,rows,fields){
          var type = rows;
          var urlList = req.url.substr(0,5);
          res.render('cake', { yuanzu: yuanzu,type:type,url:urlList,haoli:haoli, xingfu:xingfu,usertab:ut.usertab,in_out:ut.in_out,link1:ut.link1,link2:ut.link2,order:ut.order});
        })
      })
    });
  });
});
router.get('/cakeList', function(req, res, next) {
  conn.query("select count(*) as c from product where cata_id="+req.query.cata_id,function(err,rows,fields){
    req.cake_num = rows[0].c;
    next();
  });
},function(req, res, next){
  var ut= user_tab(req);
  if (req.query.page == undefined) {
    var page=1
  }else{
    page = req.query.page;
  }
  var pageSize = 8;
  var pageLength = Math.ceil(req.cake_num/pageSize);
  if (req.query.p == '1') {
    var sql = "select * from product where cata_id="+req.query.cata_id+" order by price limit "+(page-1)*pageSize+","+pageSize;
  }else if (req.query.p == '2') {
    var sql = "select * from product where cata_id="+req.query.cata_id+" order by price desc limit "+(page-1)*pageSize+","+pageSize;
  }else{
    var sql = "select * from product where cata_id="+req.query.cata_id+" limit "+(page-1)*pageSize+","+pageSize;
  }
  conn.query("select * from producttype",function(err,rows,fields){
    var type = rows;
    conn.query(sql,function(err,rows,fields){
      var urlList = req.url.substr(0,5);
      res.render('cakeList', { cake: rows ,type:type,url:urlList,page:pageLength,page_id:page-1,cake_num:req.cake_num,cata_id:req.query.cata_id,pr:req.query.p,usertab:ut.usertab,in_out:ut.in_out,link1:ut.link1,link2:ut.link2,order:ut.order});
    })
  });
});

/*礼品模块*/
router.get('/gifts', function(req, res, next) {
  var ut= user_tab(req);
  var sql = "select * from product where cata_id=24 limit 8";
  conn.query(sql,function(err,rows,fields){
    var gift_tg=rows;
    conn.query("select * from product where cata_id=25 limit 4",function(err,rows,fields){
      var gift_music=rows;
      conn.query("select * from product where cata_id=26 limit 4",function(err,rows,fields){
       var gift_shui=rows;
        conn.query("select * from product where cata_id=27 limit 4",function(err,rows,fields){
          var gift_glod=rows;
          conn.query("select * from producttype",function(err,rows,fields){
            var type = rows;
            var urlList = req.url.substr(0,5);
            res.render('gifts', { type:type,url:urlList,gift_tg: gift_tg,gift_music:gift_music,gift_shui:gift_shui, gift_glod:gift_glod,usertab:ut.usertab,in_out:ut.in_out,link1:ut.link1,link2:ut.link2,order:ut.order});
          })
        })
      })
    });
  });
});
router.get('/giftsList', function(req, res, next) {
  conn.query("select count(*) as c from product where cata_id="+req.query.cata_id,function(err,rows,fields){
    req.cake_num = rows[0].c;
    next();
  });
},function(req, res, next){
  var ut= user_tab(req);
  if (req.query.page == undefined) {
    var page=1
  }else{
    page = req.query.page;
  }
  var pageSize = 12;
  var pageLength = Math.ceil(req.cake_num/pageSize);
  if (req.query.p == '1') {
    var sql = "select * from product where cata_id="+req.query.cata_id+" order by price limit "+(page-1)*pageSize+","+pageSize;
  }else if (req.query.p == '2') {
    var sql = "select * from product where cata_id="+req.query.cata_id+" order by price desc limit "+(page-1)*pageSize+","+pageSize;
  }else{
    var sql = "select * from product where cata_id="+req.query.cata_id+" limit "+(page-1)*pageSize+","+pageSize;
  }
  conn.query("select * from producttype",function(err,rows,fields){
    var type = rows;
    conn.query(sql,function(err,rows,fields){
      var urlList = req.url.substr(0,5);
      res.render('giftsList', { gift: rows , type:type,url:urlList,page:pageLength,page_id:page-1,cake_num:req.cake_num,cata_id:req.query.cata_id,pr:req.query.p,usertab:ut.usertab,in_out:ut.in_out,link1:ut.link1,link2:ut.link2,order:ut.order});
    })
  });
});
/*巧克力模块*/
router.get('/chocolates', function(req, res, next) {
  var ut= user_tab(req);
  var sql = "select * from product where cata_id=16";
  conn.query(sql,function(err,rows,fields){
    var cho_jin=rows;
    conn.query("select * from product where cata_id=17",function(err,rows,fields){
      var cho_q=rows;
      conn.query("select * from product where cata_id=18",function(err,rows,fields){
       var cho_r=rows;
        conn.query("select * from product where cata_id=19",function(err,rows,fields){
          var cho_s=rows;
          conn.query("select * from producttype",function(err,rows,fields){
            var type = rows;
            var urlList = req.url.substr(0,5);
            res.render('chocolates', { type:type,url:urlList,cho_jin: cho_jin,cho_q:cho_q,cho_r:cho_r, cho_s:cho_s,usertab:ut.usertab,in_out:ut.in_out,link1:ut.link1,link2:ut.link2,order:ut.order});
          })
        })
      })
    });
  });
});

/*---------用户登录注册-----------*/
router.get('/login', function(req, res, next) {
  var ut= user_tab(req);
  conn.query("select * from producttype",function(err,rows,fields){
    var type = rows;
    var urlList = req.url.substr(0,5);
    res.render('login',{type:type,url:urlList,usertab:ut.usertab,in_out:ut.in_out,link1:ut.link1,link2:ut.link2,order:ut.order})
  });
});
router.post('/dologin',function(req, res, next){
  req.body.password=md5(req.body.password);
  var sql='select * from users where username="'+req.body.username+'" and password="'+req.body.password+'"';
  conn.query(sql,function(err, rows, fields){
    if(rows.length==0){
      res.send('账号或密码不正确')
    }else{
      req.session.islogin=true;
      req.session.username=req.body.username;

      for(var i = 0 ;i < rows.length ;i++){
          if (rows[i].username == req.body.username) {
            req.session.user_id=rows[i].id;
          }
      }
      req.session.password=req.body.password;
      res.send('ok');
    }
  })
})
router.get('/register', function(req, res, next) {
  var ut= user_tab(req);
  conn.query("select * from producttype",function(err,rows,fields){
    var type = rows;
    var urlList = req.url.substr(0,5);
    res.render('register',{type:type,url:urlList,usertab:ut.usertab,in_out:ut.in_out,link1:ut.link1,link2:ut.link2,order:ut.order})
  });
});
router.post('/doregister',function(req, res, next){
  req.body.password=md5(req.body.password);
  req.body.password_again=md5(req.body.password_again);
  var sql='insert into users values(null,"'+req.body.username+'","'+req.body.password+'","'+req.body.password_again+'")';
    if(req.body.identify!=req.body.codese) {
     res.send('您输入的验证码不正确')
    }else if(req.body.password_again!=req.body.password) {
       res.send('两次输入密码不一致')
    }else if(req.body.username==0){
       res.send('用户名不能为空！')
    }else{
      req.session.islogin=true;
      req.session.username=req.body.username;
      conn.query(sql,function(err, rows, fields){   
        res.send("ok");     
      })
    }
 })
router.get('/loginOut', function(req, res, next) {
  var ut= user_tab(req);
  req.session.destroy();
  res.redirect('login')
});
/*-----------商品购买-详情页------------*/
router.get('/buy', function(req, res, next) {
  var ut= user_tab(req);
    conn.query('select count(*) from shopping',function(err, rows, fields){
      req.shopping_num = rows[0]['count(*)'];
      conn.query("select * from producttype",function(err,rows,fields){
        var type = rows;
        var urlList = req.url.substr(0,5);
        var sql='select * from product where id='+req.query.id;
        conn.query(sql,function(err, rows, fields){
          res.render('buy',{type:type,url:urlList,buy:rows[0],shopping_num:req.shopping_num,usertab:ut.usertab,in_out:ut.in_out,link1:ut.link1,link2:ut.link2,order:ut.order})
        })
      })
  })
});
/*------------订单页面----------------*/
router.get('/fillOrder', function(req, res, next) {
  var sql='select * from product where id='+req.query.id;
  conn.query(sql,function(err, rows, fields){
    res.render('fillOrder',{buy:rows[0]})
  })
});
router.post('/dofillOrder',function(req, res, next){
  var sql='insert into order_infro values(null,"'+req.body.receiver+'","'+req.body.amount+'","'+req.body.deliveryTime+'","'+req.body.allState+'")';
    conn.query(sql,function(err, rows, fields){
      res.send("下单成功！");
    })
 })
/*-----------购物车------------*/
router.get('/shopping', function(req, res, next) {
  var ut= user_tab(req);
  var sql='select * from shopping';
  conn.query("select * from producttype",function(err,rows,fields){
    var type = rows;
    var urlList = req.url.substr(0,5)
    conn.query(sql,function(err, rows, fields){
      res.render('shopping',{type:type,url:urlList,buy:rows,usertab:ut.usertab,in_out:ut.in_out,link1:ut.link1,link2:ut.link2,order:ut.order})
    }) 
  });
});
router.post('/doshopping',function(req, res, next){
 var sql='insert into shopping values(null,"'+req.body.abstract+'","'+req.body.price+'")';
    conn.query(sql,function(err, rows, fields){   
     conn.query('select count(*) from shopping',function(err, rows, fields){   
      req.shopping_num = rows[0]['count(*)'];
     res.send(""+req.shopping_num);
    })
  })
})
router.get('/delshopping', function(req, res, next) {
  var ut= user_tab(req);
  conn.query('delete from shopping where id='+req.query.id, function(err, rows, fields){
  res.redirect("shopping");
  })
});
router.get('/del_all_shopping', function(req, res, next) {
  var ut= user_tab(req);
  conn.query('delete from shopping', function(err, rows, fields){
  res.redirect("shopping");
  })
});


/*测试*/
router.get('/tjg', function(req, res, next) {
  conn.query("select * from producttype",function(err,rows,fields){
    res.render('tjg', {type:rows});
  })
});


module.exports = router;
